//
//  NewsListViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/3.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "NewsListViewModel.h"

#import "CommonItemViewModel.h"
#import "CommonNewsItemViewModel.h"

#import "ArticlesListData.h"

@interface NewsListViewModel()

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation NewsListViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.areaID = @"0";
    
    self.cellHeight = 94.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = NO;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath* indexPath)
    {
        @strongify(self);
        
        id ivm = self.dataSource[indexPath.row];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
                [subscriber sendNext:ivm];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        NSDictionary* postData = self.type == 0 ? interface_param_getcatelist(@"56", self.areaID, @(page+1), @(List_PageSize)) : interface_param_getcatelist(@"57", self.areaID, @(page+1), @(List_PageSize));
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_getcatelist params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    ArticlesListData* alistData = [ArticlesListData modelWithJSON:x];
                    
                    NSArray *viewModels = [alistData.datas.rac_sequence map:^CommonNewsItemViewModel *(ArticleListData* data)
                                           {
                                               @strongify(self);
                                               
                                               id resultIVM = nil;
                                               
                                               if (self.type == 0)
                                               {
                                                   resultIVM = [[CommonItemViewModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               }
                                               else if (self.type == 1)
                                               {
                                                   resultIVM = [[CommonNewsItemViewModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               }
                                               
                                               return resultIVM;
                                           }].array;
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == page)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
